/*
 * SimpleWaitCondition.java  0.4.0 / Oct 23, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.util;

/**
 * Simple Condition Synchronizer.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class SimpleWaitCondition {

// Private Fields. -------------------------------------------------------------

  private final Object lock;
  private boolean signaled;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new SimpleWaitCondition object.
   */
  public SimpleWaitCondition() {
    lock = new Object();
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns <tt>true</tt> if the condition is signaled.
   *
   * @return <tt>true</tt> if the condition is signaled
   */
  public boolean isSignaled() {
    synchronized (lock) {
      return signaled;
    }
  }

  /**
   * Waits for condition.
   */
  public void waitFor() {
    synchronized (lock) {
      try {
        while (!signaled) {
          lock.wait();
        }
      } catch (InterruptedException ex) {
        // empty catch block
      }
    }
  }

  /**
   * Signals the condition.
   */
  public void signal() {
    synchronized (lock) {
      signaled = true;
      lock.notifyAll();
    }
  }

}
